package com.springboot.demo.service.impl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.demo.entity.po.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description：
 * @Date： Created in 2021/12/30
 * @author： jianbin.lei
 * @Auto： <a href="mailto:493168771@qq.com">jianbin.lei</a>
 */
@Mapper
@Repository
@CacheConfig(cacheNames = {"sysUsers"})
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 获取后台用户信息列表
     * @param userName 用户名
     * @return
     */
    @Cacheable
    List<SysUser> selectUserList(@Param("userName") String userName);

    /**
     * 根据用户名, 获取用户信息
     *
     * @param sUserName 用户名
     * @return
     */
    @Select("SELECT u.* FROM sys_user u  " +
            "            WHERE u.user_name = #{0} AND u.del_flag = 0 ")
    SysUser selectUserByUserName(String sUserName);

    /**
     * 更新最近登录时间
     *
     * @param iUserId 用户ID
     * @return iCnt
     */
    @Update("UPDATE sys_user SET last_online_time = current_timestamp WHERE id = #{0}")
    Integer updateLastOnlineTime(Long iUserId);

    /**
     * 根据用户id获取用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @Select(
            "SELECT u.*, rtrim(xmlagg(XMLELEMENT(e,NVL(r.role_name,''),',').EXTRACT('//text()')).getclobval(),',') as role_name "
                    + " , rtrim(xmlagg(XMLELEMENT(e,NVL(r.id,''),',').EXTRACT('//text()')).getclobval(),',') as role_id "
                    + "FROM sys_user u "
                    + "LEFT OUTER JOIN sys_user_role a ON u.id = a.user_id AND a.del_flag = 0 "
                    + "LEFT OUTER JOIN sys_role r ON a.role_id = r.id AND r.del_flag = 0 "
                    + "WHERE u.del_flag = 0 AND u.id = #{0} "
                    + "GROUP BY u.ID, u.USER_NAME, u.PASSWORD, u.REAL_NAME, u.AVATAR, u.CONTACT_PHONE_NO, u.LAST_ONLINE_TIME, u.DEL_FLAG, u.CREATE_TIME, u.CREATE_USER, u.UPDATE_TIME, u.UPDATE_USER")
    SysUser selectUserDetail(Long userId);

    /**
     * 根据用户名，获取用户件数
     *
     * @param userName 用户名
     * @return 用户件数
     */
    @Select("SELECT COUNT(1) FROM sys_user WHERE user_name = #{userName} AND del_flag = 0")
    Integer selectCountByUserName(@Param("userName")String userName);

    /**
     * 根据用户名和用户ID，获取用户件数
     *
     * @param iUserId 用户ID
     * @param userName 用户名
     * @return 用户件数
     */
    @Select("SELECT COUNT(1) FROM sys_user WHERE id != #{iUserId} AND user_name = #{userName} AND del_flag = 0")
    Integer selectCountByUserIdAndName(@Param("iUserId")Long iUserId, @Param("userName")String userName);

    /**
     * 更新用户是否删除标志位
     *
     * @param iUserId
     * @param userId
     * @return
     */
    @Update("UPDATE sys_user SET del_flag = 1, update_time = CURRENT_TIMESTAMP, update_user = #{1} WHERE id = #{0}")
    Integer updateUserDelFlag(Long iUserId, Long userId);



}
